/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.dts.oo.io;

import com.sun.star.io.*;
import com.sun.star.lang.IllegalArgumentException;

/**
 * 过时发现效率太低
 * <p/>
 * OOo loadComponent 中 PropertyValue 中 InputStream 属性要求必须实现 XInputStream 和 XSeekable 接口
 * 其提供的 ByteArrayToXInputStreamAdapter 实现 效率较低, 而且在加载大文件容易溢出
 * 而提供的 InputStreamToXInputStream 并没有实现 XSeekable 接口
 * {@link com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter}
 *
 * @author vacoor
 * @see <decode href="http://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html#InputStream" />
 * @deprecated
 */
public class SeekableInputStreamToXSeekableInputStreamAdapter implements XInputStream, XSeekable {
    protected SeekableInputStream in;

    public SeekableInputStreamToXSeekableInputStreamAdapter(SeekableInputStream in) {
        this.in = in;
    }

    @Override
    public int readBytes(byte[][] bytes, int len) throws NotConnectedException, BufferSizeExceededException, IOException {
        try {
            if (bytes[0] == null) {
                bytes[0] = new byte[len];
            }
            return in.read(bytes[0], 0, len);
        } catch (Exception e) {
            throw new IOException(e.toString());
        }
    }

    @Override
    public int readSomeBytes(byte[][] bytes, int len) throws NotConnectedException, BufferSizeExceededException, IOException {
        return readBytes(bytes, len);
    }

    @Override
    public void skipBytes(int i) throws NotConnectedException, BufferSizeExceededException, IOException {
        try {
            in.skip(i);
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }

    @Override
    public int available() throws NotConnectedException, IOException {
        try {
            return in.available();
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }

    @Override
    public void closeInput() throws NotConnectedException, IOException {
        try {
            in.close();
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }

    //
    @Override
    public void seek(long pos) throws IllegalArgumentException, IOException {
        try {
            in.seek(pos);
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }

    @Override
    public long getPosition() throws IOException {
        try {
            return in.position();
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }

    @Override
    public long getLength() throws IOException {
        try {
            return in.length();
        } catch (java.io.IOException e) {
            throw new IOException(e.toString());
        }
    }
}
